package garbage;

import java.io.IOException;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import garbage.Utilisateur;
import garbage.PMF;

public class GarbageTimeServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(GarbageTimeServlet.class.getName());

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException, ServletException {
    	UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        HttpSession session = req.getSession(true);
        String pathInfo = req.getPathInfo();
        
        if(pathInfo.equals("/start")){
        	String nom = req.getParameter("nom");
        	String prenom = req.getParameter("prenom");
        	Utilisateur util = new Utilisateur(user, nom, prenom);
        	PersistenceManager pm = PMF.get().getPersistenceManager();
            try {
                pm.makePersistent(util);
            } finally {
                pm.close();
            }
        	RequestDispatcher rd = 	getServletContext().getRequestDispatcher("/start.jsp");
        	rd.forward(req, resp);
        }
    }
}